package com.ztmg.water.service.bills.impl;

import com.ztmg.common.constant.CommonConstants;
import com.ztmg.water.mapper.bills.WaterStockMapper;
import com.ztmg.water.model.bills.PutInBills;
import com.ztmg.water.model.bills.WaterStock;
import com.ztmg.water.model.bills.WaterStockExample;
import com.ztmg.water.vo.bills.PutInBillsVO;
import com.ztmg.water.dto.bills.PutInBillsDTO;
import com.ztmg.water.dto.bills.PutInBillsPageDTO;
import com.ztmg.common.util.resultUtils.JsonResult;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ztmg.common.constant.ErrorConst;
import org.springframework.beans.BeanUtils;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
import com.ztmg.water.service.bills.PutInBillsService;
import com.ztmg.water.mapper.bills.PutInBillsMapper;import com.ztmg.water.model.bills.PutInBillsExample;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Service
public class PutInBillsServiceImpl implements PutInBillsService{

    @Resource
    private PutInBillsMapper putInBillsMapper;
    @Resource
    private WaterStockMapper  waterStockMapper;
    @Override
    public JsonResult create(PutInBillsDTO dto) {
        if(null != dto){
            PutInBills bean = new PutInBills();
            BeanUtils.copyProperties(dto,bean);
            bean.setCreateTime(new Date());
            //后续完善
            bean.setCreater("");
            putInBillsMapper.insert(bean);
            return JsonResult.success();
        }else {
            return JsonResult.fail(ErrorConst.SYSTEM_PARM_ERROR,ErrorConst.SYSTEM_PARM_ERROR_MSG);
        }
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED,isolation = Isolation.DEFAULT,timeout=36000,rollbackFor=Exception.class)
    public JsonResult createPutInBills(PutInBillsDTO dto) {
        if(null != dto){
            PutInBills bean = new PutInBills();
            BeanUtils.copyProperties(dto,bean);
            bean.setCreateTime(new Date());
            //后续完善
            bean.setCreater("");
            putInBillsMapper.insert(bean);
            /**将当前入库单对应的水数量添加入总库存中**/
            WaterStockExample example = new WaterStockExample();
            WaterStockExample.Criteria criteria =  example.createCriteria();
            criteria.andWaterIdEqualTo(bean.getWaterId());
            List<WaterStock> waterStockList = waterStockMapper.selectByExample(example);

            if(waterStockList.size()>0)
            {
                PutInBills updateWaterNumsParams = new PutInBills();
                updateWaterNumsParams.setWaterId(bean.getWaterId());
                updateWaterNumsParams.setWaterNum(bean.getWaterNum());
                updateWaterNumsParams.setModifier("");
                updateWaterNumsParams.setModifyTime(new Date());
                //修改水库存数量
                waterStockMapper.updateWaterNumsByWaterId(updateWaterNumsParams);
            }else{
                //添加库存记录
                WaterStock waterStock = new WaterStock();
                waterStock.setCreater("");
                waterStock.setCreateTime(new Date());
                waterStock.setModifier("");
                waterStock.setModifyTime(new Date());
                waterStock.setRemark("");
                waterStock.setStatus(CommonConstants.TABLE_STATUS_1);
                waterStock.setWaterEmptyNum(0);
                waterStock.setWaterId(bean.getWaterId());
                waterStock.setWaterStock(bean.getWaterNum());
                waterStockMapper.insert(waterStock);
            }
            return JsonResult.success();
        }else {
            return JsonResult.fail(ErrorConst.SYSTEM_PARM_ERROR,ErrorConst.SYSTEM_PARM_ERROR_MSG);
        }
    }

    @Override
    public JsonResult update(PutInBillsDTO dto) {
        if(null != dto && null != dto.getId()){
            PutInBills bean = new PutInBills();
            BeanUtils.copyProperties(dto,bean);
            bean.setModifyTime(new Date());
            //后续完善
            bean.setModifier("");
            putInBillsMapper.updateByPrimaryKeySelective(bean);
            return JsonResult.success();
        }else {
            return JsonResult.fail(ErrorConst.SYSTEM_PARM_ERROR,ErrorConst.SYSTEM_PARM_ERROR_MSG);
        }
    }

    @Override
    public JsonResult<PutInBillsVO> selectById(Integer id) {
        if(null != id){
            PutInBills bean = putInBillsMapper.selectByPrimaryKey(id);
            PutInBillsVO vo = new PutInBillsVO();
            if(null != bean){
                BeanUtils.copyProperties(bean,vo);
            }else {
                return JsonResult.fail(ErrorConst.SYSTEM_PARM_ERROR,ErrorConst.SYSTEM_PARM_ERROR_MSG);
            }
            return JsonResult.success(vo);
        }else{
            return JsonResult.fail(ErrorConst.SYSTEM_PARM_ERROR,ErrorConst.SYSTEM_PARM_ERROR_MSG);
        }
    }

    @Override
    public JsonResult<PutInBillsVO> select(PutInBillsDTO dto) {
        PutInBillsExample example = new PutInBillsExample();
        PutInBillsExample.Criteria criteria =  example.createCriteria();
        /***
        * 根据具体的业务，查询条件赋值
         */
        List<PutInBills> sysUserList = putInBillsMapper.selectByExample(example);
        PutInBillsVO vo = new PutInBillsVO();
        if(!sysUserList.isEmpty()){
            BeanUtils.copyProperties(sysUserList.get(0),vo);
        }
        return JsonResult.success(vo);
    }

    @Override
    public JsonResult<PageInfo<PutInBillsVO>> selectPage(PutInBillsPageDTO dto) {
        PutInBillsExample example = new PutInBillsExample();
        PutInBillsExample.Criteria criteria =  example.createCriteria();
        /***
        * 根据具体的业务，查询条件赋值
         */
        PageHelper.startPage(dto.getPage(), dto.getLimit());
        List<PutInBills> beanList = putInBillsMapper.selectByExample(example);
        PageInfo pageInfo = new PageInfo<>(beanList);
        List<PutInBillsVO> respDTOS = beanList.parallelStream().map((bean) -> {
            PutInBillsVO vo = new PutInBillsVO();
            BeanUtils.copyProperties(bean, vo);
            return vo;
        }).collect(Collectors.toList());
        pageInfo.setList(respDTOS);
        return JsonResult.success(pageInfo);
    }

    @Override
    public JsonResult deleteById(Integer id) {
        return JsonResult.success( putInBillsMapper.deleteByPrimaryKey(id));
    }

    @Override
    public JsonResult delete(PutInBillsDTO dto) {
        PutInBillsExample example = new PutInBillsExample();
        PutInBillsExample.Criteria criteria =  example.createCriteria();
        /***
        * 根据具体的业务，查询条件赋值
         */
        return JsonResult.success(putInBillsMapper.deleteByExample(example));
    }
}